$(document).ready(function() { 
    $(".tablesorter").tablesorter();
    
    $('form').set_mascara();
    
    //When page loads...
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
        return false;
    });
                
    $('.column').equalHeight();
		
    $('input[type="submit"]').live('click',function(e){
        e.preventDefault();
        var obj = $(this).closest('form');
        $(obj).form_validate({
            success:function(z,x,y){
                if($.trim($(obj).attr('action')) != ''){
                    $.ajax({
                        type: "POST",
                        data: $(x).serialize(),
                        dataType: "json",
                        url: $(x).attr('action'),
                        beforeSend: function () {
                            msg_sistema('Aguarde... Executando operação.','info');
                        },
                        success: function(b) {
                            if(b.cod == 999){
                                $(x).find('#'+$(x).attr('id_hidden')).val(b.id);
                                msg_sistema(b.msg,'success');
                                $(x).find(':input').removeClass('textoSucesso')
                            } else {
                                msg_sistema(b.msg,'error');
                            }
                            
                        }
                    });
                } else {
                    msg_sistema('Action do formulario não definido.','error');
                }  
            },
            error:function(z,x,y){
                msg_sistema(z.msg,'error');
            }
        });
        
    });
    
    $('textarea.editor').redactor({
        imageUpload: base_adm+'midias/midia/img',
        fileUpload: base_adm+'midias/midia/file',
        imageGetJson: base_adm+'midias/midia/images_json',
        lang: 'pt_br'
    });
    
    $('.upload').jUpload({
        url: base_adm+'midias/midia/gravar',
        success : function(a,b){
            msg_sistema(a.msg,'success');
            window.location.href=window.location.href;
        }, 
        error : function(a,b){
            msg_sistema(a.msg,'error');
        }
    });
    
    $('a.dialog_detalhes').click(function(e){
        e.preventDefault();
        var obj = this;
        $.ajax({
            type: "GET",
            dataType: "html",
            url: $(obj).prop('href'),
            success: function(b) {
                if($('#dialog-message').length == 0){
                    $('body').append('<div id="dialog-message"><p></p></div>');
                }
                $( "#dialog:ui-dialog" ).dialog( "destroy" );
                $( "#dialog-message p" ).html(b);
                $( "#dialog-message" ).dialog({
                    modal: true,
                    title: 'Detalhes da Midia',
                    width : $(window).width() - 100,
                    height : $(window).height() - 100,
                    buttons: {
                        Ok: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
        });
    });
    
    $('a.dialog_excluir').click(function(e){
        e.preventDefault();
        var obj = this;
        if($('#dialog-message').length == 0){
            $('body').append('<div id="dialog-message"><p></p></div>');
        }
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message p" ).html('Deseja excluir este registro.');
        $( "#dialog-message" ).dialog({
            modal: false,
            title: 'Exclusão',
            width : 350,
            height : 180,
            buttons: {
                'Sim': function() {
                    var obj2 = this;
                    
                    $.ajax({
                        type: "GET",
                        dataType: "json",
                        url: $(obj).prop('href'),
                        success: function(b) {
                            $( obj2 ).dialog( "close" );
                            if(b.cod == 111){
                                msg_sistema(b.msg, 'error');
                            } else {
                                msg_sistema(b.msg, 'success');
                                $(obj).closest('tr').remove();
                            }
                            
                        }
                    });
                }, 
                "Não": function() {
                    $( this ).dialog( "close" );
                }
            }
        });
    });
    
});
